<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<style>
    .test{
            background: green;
            height: 30px;
    }
    .test2{
            background: red;
            line-height: 30px;
            border: 1px solid goldenrod;
    }
    .test2{
        background-color: yellow;
    }
</style>

<body>
    <p class="test">
        这个是用来测试JS动态添加class
    </p>
</body>
<script type="text/x-tmpl" id="main_info">
    <div>
     <h5>
        <p>这是模板代码</p>
     </h5>
   </div>
</script>
<script>
    window.onload = function () {
        // 动态添加class 方法 
        let testDemo = document.getElementsByClassName("test")[0];
        // 使用传统className 再拼接上一个新的类名
        testDemo.className += " test1";
        // 使用HTML5：classList 一个 DOMTokenList 保存着该节点的所有类名 已经含有add方法添加类名 remove 删除类名的操作
        testDemo.classList.add("test2")
        // 移除一个类名
        testDemo.classList.remove("test");
        console.log(testDemo.classList);
        console.log(testDemo.className);

        var infoTmpl = document.getElementById("main_info").innerHTML;
        console.log(infoTmpl);

        let main_obj = (function () {
            let name = "whmk";
            let age = "20";
            let sayName = function () {
                console.log(name);
            };
            let sayAge = function () {
                console.log(age);
            }
            return {
                sayName: sayName,
                sayAge: sayAge
            }
        })();

        let a = main_obj;
        console.log(a);
        a.sayName();

        let main_obj_2 = function (name2, age2) {
            let name = name2 + 'whmk';
            let age = age2 - 2;
            let sayName = function () {
                console.log(name);
            };
            let sayAge = function () {
                console.log(age);
            }
            return {
                sayName: sayName,
                sayAge: sayAge
            }
        };

        let b = main_obj_2("bbb", 22);
        console.log(b);
        b.sayName();
        b.sayAge();
    }
</script>



</html>